package com.huihe.sys.controller;

import cn.hutool.core.util.StrUtil;
import com.huihe.common.entity.AjaxResult;
import com.huihe.common.enums.SysConfigEnum;
import com.huihe.storage.dto.RegisterBody;
import com.huihe.framework.service.RegisterService;
import com.huihe.framework.service.SysConfigService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录 Controller层
 */
@RestController
@AllArgsConstructor
public class RegisterController {

    /**
     * 参数配置 Service 层
     */
    SysConfigService sysConfigService;

    /**
     * 注册 Service 层
     */
    RegisterService registerService;

    /**
     * 注册
     * @param registerBody 注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody registerBody){
        //判断是否允许注册:   键:sys.account.registerUser  值:  0:允许 1:不允许
        if (sysConfigService.findByKey(SysConfigEnum.Allowed_Register.getConfigKey()).getConfigValue().equals("1")){
            return AjaxResult.error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(registerBody);
        return StrUtil.isBlank(msg) ? AjaxResult.success() : AjaxResult.error(msg);
    }
}